package com.hzj.controller;

import com.hzj.utils.RedisUtil;
import com.hzj.utils.Result;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/platformapi/sms/")
public class SmsController {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    RedisUtil redisUtil;


    @PostMapping("/send/{mobile}")
    public Result verificationMail(@PathVariable String mobile) {
//        生成4位随机数
        String random = RandomStringUtils.random(4,false,true);

//        发送验证码
        sendmail(random,mobile);

        //        验证码存到redis
        redisUtil.set("userCode|"+mobile,random,120);
        System.out.println("返回值");
        return Result.ok();
    }

    @Async
    public void sendmail(String random,String mobile){


        //        发送验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("便医通验证码");//标题
        message.setText("您的验证码为:" + random);// 内容
        message.setFrom("1733013921@qq.com");//从哪个邮箱
        message.setTo(mobile);//发送到哪个邮箱
        mailSender.send(message);
    }

}
